package designpattern.proxy;


import util.GsonUtil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * JDK代理对象的类
 * @Author Antony
 * @Since 2018/6/7 14:55
 */
public class JDKProxy implements InvocationHandler {

    private Object proxyObject;
    private String proxyName;

    public JDKProxy(Object proxyObject) {
        this.proxyObject = proxyObject;
    }

    public JDKProxy(Object proxyObject, String proxyName) {
        this.proxyObject = proxyObject;
        this.proxyName = proxyName;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // do something
        Object ret = null;  //方法返回值

        Class proxyClazz = proxy.getClass();
        System.out.println(proxy.getClass().getName());
        System.out.println("JDK Proxy --- 调用前 --- 调用方法是:"+method.getName() + "---参数是:"+ GsonUtil.toJson(args));

        ret = method.invoke(proxyObject, args); //调用invoke方法

        System.out.println("JDK Proxy --- 调用后");
        return ret;
    }
}
